#include "test_mqtt.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "cm_mem.h"
#include <stdio.h>
#include "cm_ssl.h"

#include <string.h>
#include "stdlib.h"
#include "stdint.h"

#define MQTT_TRAVERSAL_SIZE  1000    //遍历长度

/* SUB订阅主题 */
static char *cmmqtt_glob_subtopic_onenet__ = "dev/gsm/u";
static char *cmmqtt_glob_subtopic_onenet_ = "$sys/3JHxkADEQN/ML305A_OC_DS_DC/custome/down/100/+";
//static char *cmmqtt_glob_subtopic_onenet_tls = "$sys/452280/SMRML302/dp/post/json/+";
static char *cmmqtt_glob_subtopic_onenet_tls = "$sys/339283/ML30X_TEST/dp/post/json/+";

static char *cmmqtt_glob_willlwt_subtopic_test = "{\\\"lwt12\\\":\"mqtt_test\"}";

/* SUB订阅主题QoS */
static int cmmqtt_glob_subqos = 1;

/* PUBLISH发布主题 */
static char *cmmqtt_glob_pubtopic_onenet_ = "$sys/3JHxkADEQN/ML305A_OC_DS_DC/custome/up";
static char *cmmqtt_glob_pubtopic_onenet_tls = "$sys/339283/ML30X_TEST/dp/post/json";
/* PUBLISH发布主题QoS */
static int cmmqtt_glob_pubqos = 1;
/* PUBLISH发布主题retain参数 */
static int cmmqtt_glob_pubretain = 0;
/* PUBLISH发布主题dup */
static int cmmqtt_glob_pubdup = 0;
/* PUBLISH发布主题message */
//static char cmmqtt_glob_pubmessage_4k[10] = {'a',0x00,'b',0x00,'c',0x00,'d',0x00,'e','3'};
//全局变量，为节省空间，提供给其他模块使用
char *cmmqtt_glob_pubmessage_4k = "\\\"1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxc"\
                                      "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxc"\
                                      "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxc"\
                                      "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxc\\\"";
static char *cmmqtt_glob_pubmessage_1k = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxc";
static char *cmmqtt_glob_pubmessage_str = " !?#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvw xyz{|}~end1234";

static char *cmmqtt_glob_pubmessage_hex = "434D494F545F4F6E654D4F";
static char *cmmqtt_glob_pubmessage_json = "{ \"id\": 123, \"dp\": { \"temperatrue\": [{ \"v\": 30, }], \"power\": [{ \"v\": 4.5, }] } }";

static char *cmmqtt_glob_willlwt_pubmessage_str = "{\\\"reqId\\\":0,\"msgType\":\"Lwt\",\"sn\":\"866156051511874\",\"err\":0}";

/* 服务器地址 */
static char cmqtt_glob_cfgserver_onenet__[129] = "183.230.40.39";
static char cmqtt_glob_cfgserver_onenet_[129] = "mqtts.heclouds.com";
//static char cmqtt_glob_cfgserver_onenet_tls[129] = "183.230.40.16";
static char cmqtt_glob_cfgserver_onenet_tls[129] = "mqttstls.heclouds.com";

//static char cmqtt_glob_cfgserver_test[129] = "39.106.55.121";
static char cmqtt_glob_cfgserver_test[129] = "2408:400a:16a:9700:d3af:3f8b:7c2b:8b84";
/* port号，非加密 */
static int cmqtt_glob_cfgport_onenet__ = 6002;//"183.230.40.39" port
static int cmqtt_glob_cfgport_onenet_ = 1883;//"mqtts.heclouds.com" port
static int cmqtt_glob_cfgport_test = 1883;

/* port号，加密 */
static int cmqtt_glob_cfgport_onenet_tls = 8883;
static int cmqtt_glob_cfgport_test_tls = 8883;

/* client_id */
//static char cmqtt_glob_cfgclient_id_onenet[129] = "687804729";
//static char cmqtt_glob_cfgclient_id_onenet[129] = "768046506";//"183.230.40.39" id
static char cmqtt_glob_cfgclient_id_onenet_[129] = "ML305A_OC_DS_DC";
static char cmqtt_glob_cfgclient_id_onenet__[129] = "984908940";
//static char cmqtt_glob_cfgclient_id_onenet_tls[129] = "SMRML302";
static char cmqtt_glob_cfgclient_id_onenet_tls[129] = "ML30X_TEST";

static char *cmqtt_glob_cfgclient_id_test1 = "test1 id_111111111111111111111";
static char *cmqtt_glob_cfgclient_id_test2 = "test2 id_1111111111111111111111";

/* keepAlive参数 */
static int cmqtt_glob_cfgkeepAlive = 60;

/* 用户名 */
static char cmqtt_glob_cfguser_onenet__[129] = "318975";//"183.230.40.39" username
static char cmqtt_glob_cfguser_onenet_[129] = "3JHxkADEQN";
static char cmqtt_glob_cfguser_onenet_tls[129] = "339283";

static char *cmqtt_glob_cfguser_test1 = "test1 user_111111111111111111";
static char *cmqtt_glob_cfguser_test2 = "test2 user_1111111111111111111";

/* 密码 */
static char cmqtt_glob_cfgpasswd_onenet__[129] = "869324052384520";
//static char cmqtt_glob_cfgpasswd_onenet_[129] = "866777520128830";//"183.230.40.39" password
static char cmqtt_glob_cfgpasswd_onenet_[512] = "version=2018-10-31&res=products%2F3JHxkADEQN%2Fdevices%2FML305A_OC_DS_DC&et=1715063576&method=md5&sign=SNaZvVfRa5d%2BN39TaaHIvw%3D%3D";
//static char cmqtt_glob_cfgpasswd_onenet_tls[512] = "version=2018-10-31&res=products%2F452280%2Fdevices%2FSMRML302&et=1948443117&method=md5&sign=RPIFpBdAKRl6jk%2BbB6pKWg%3D%3D";

static char cmqtt_glob_cfgpasswd_onenet_tls[512] = "version=2018-10-31&res=products%2F339283%2Fdevices%2FML30X_TEST&et=1923380302&method=md5&sign=JhdU%2F%2BYwAGS8j7rjt5LDxw%3D%3D";

static char *cmqtt_glob_cfgpasswd_test1 = "test1 passwd_111111111111111111111111111111111111";
static char *cmqtt_glob_cfgpasswd_test2 = "test2 passwd_1111111111111111111111111111111111111";

/* 是否清除会话 */
static int cmqtt_glob_cfgclean = 1;
/* 是否使用MQTTS参数；0：不使用，1：单向认证，2：双向认证 */
static int cmqtt_glob_cfgencrypt = 0;

/* 是否使用user参数；0不使用，1使用； */
static int cmmqtt_glob_opusrflag = 0;
/* 是否使用password参数；0不使用，1不使用； */
static int cmmqtt_glob_oppwdflag = 0;

/* 确认是否需要使用will LWT机制；0关闭，1开启；*/
static int cmmqtt_glob_opwillflag = 0;
/* 服务器是否保存 will 消息（0-1）；0不保存，1保存；Will遗言机制参数 */
static int cmmqtt_glob_opretainflag = 0;
/* Will遗言机制QoS参数；Will遗言机制参数 */
static int cmmqtt_glob_opwillQos = 0;
/* 连接异常终止时发送消息到相应topic；Will遗言机制参数*/
static char *cmmqtt_glob_opwilltopic = "";
/* 连接异常终止时发送的消息；Will遗言机制参数*/
static char *cmmqtt_glob_opwill_msg = "";

/** An enumeration of the PUBLISH flags. */
typedef enum
{
    CM_MQTT_PUBLISH_DUP = 8u,
    CM_MQTT_PUBLISH_QOS_0 = ((0u << 1) & 0x06),
    CM_MQTT_PUBLISH_QOS_1 = ((1u << 1) & 0x06),
    CM_MQTT_PUBLISH_QOS_2 = ((2u << 1) & 0x06),
    CM_MQTT_PUBLISH_QOS_MASK = ((3u << 1) & 0x06),
    CM_MQTT_PUBLISH_RETAIN = 0x01
} cm_mqtt_publish_flags_e;

#define CM_MQTT_CLIENT_MAX          5

unsigned char *onenet_ca_cert = "-----BEGIN CERTIFICATE-----\n"\
"MIIDOzCCAiOgAwIBAgIJAPCCNfxANtVEMA0GCSqGSIb3DQEBCwUAMDQxCzAJBgNV\n"\
"BAYTAkNOMQ4wDAYDVQQKDAVDTUlPVDEVMBMGA1UEAwwMT25lTkVUIE1RVFRTMB4X\n"\
"DTE5MDUyOTAxMDkyOFoXDTQ5MDUyMTAxMDkyOFowNDELMAkGA1UEBhMCQ04xDjAM\n"\
"BgNVBAoMBUNNSU9UMRUwEwYDVQQDDAxPbmVORVQgTVFUVFMwggEiMA0GCSqGSIb3\n"\
"DQEBAQUAA4IBDwAwggEKAoIBAQC/VvJ6lGWfy9PKdXKBdzY83OERB35AJhu+9jkx\n"\
"5d4SOtZScTe93Xw9TSVRKrFwu5muGgPusyAlbQnFlZoTJBZY/745MG6aeli6plpR\n"\
"r93G6qVN5VLoXAkvqKslLZlj6wXy70/e0GC0oMFzqSP0AY74icANk8dUFB2Q8usS\n"\
"UseRafNBcYfqACzF/Wa+Fu/upBGwtl7wDLYZdCm3KNjZZZstvVB5DWGnqNX9HkTl\n"\
"U9NBMS/7yph3XYU3mJqUZxryb8pHLVHazarNRppx1aoNroi+5/t3Fx/gEa6a5PoP\n"\
"ouH35DbykmzvVE67GUGpAfZZtEFE1e0E/6IB84PE00llvy3pAgMBAAGjUDBOMB0G\n"\
"A1UdDgQWBBTTi/q1F2iabqlS7yEoX1rbOsz5GDAfBgNVHSMEGDAWgBTTi/q1F2ia\n"\
"bqlS7yEoX1rbOsz5GDAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAL\n"\
"aqJ2FgcKLBBHJ8VeNSuGV2cxVYH1JIaHnzL6SlE5q7MYVg+Ofbs2PRlTiWGMazC7\n"\
"q5RKVj9zj0z/8i3ScWrWXFmyp85ZHfuo/DeK6HcbEXJEOfPDvyMPuhVBTzuBIRJb\n"\
"41M27NdIVCdxP6562n6Vp0gbE8kN10q+ksw8YBoLFP0D1da7D5WnSV+nwEIP+F4a\n"\
"3ZX80bNt6tRj9XY0gM68mI60WXrF/qYL+NUz+D3Lw9bgDSXxpSN8JGYBR85BxBvR\n"\
"NNAhsJJ3yoAvbPUQ4m8J/CoVKKgcWymS1pvEHmF47pgzbbjm5bdthlIx+swdiGFa\n"\
"WzdhzTYwVkxBaU+xf/2w\n"\
"-----END CERTIFICATE-----\n";

static cm_mqtt_client_t *_mqtt_client[CM_MQTT_CLIENT_MAX] = {0};

/**
 *  \brief 连接状态回调
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] session session标志
 *  \param [in] conn_res 连接状态
 *  \return 成功返回0，失败返回-1
 *  
 *  \details More details
 */
static int __mqtt_manager_default_connack_cb(cm_mqtt_client_t* client, int session, cm_mqtt_conn_state_e conn_res)
{

    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }
    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , CONNECT: %d\r\n", i, conn_res);
           
    return 0;
}

/**
 *  \brief server->client发布消息回调
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] msgid 消息ID
 *  \param [in] topic 主题
 *  \param [in] payload 负载
 *  \param [in] payload_len 负载长度
 *  \param [in] total_len 负载总长度
 *  \return 成功返回0，失败返回-1
 *  
 *  \details ToDo：增加消息缓存模式
 */
static int __mqtt_manager_default_publish_cb(cm_mqtt_client_t* client, unsigned short msgid, char* topic, int total_len, int payload_len, char* payload)
{
    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }

    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , recv: %d,%s,%d,%d\r\n", i, msgid, topic, total_len, payload_len);
    //i = 0;
    // for(;i<payload_len;)
    // {
    // cm_demo_printf("%#x ",payload[i]);
    // i++;
    // }
// #if defined(CM_OPENCPU_MODEL_ML302)
//     for(;i<payload_len;)
//     {
//         cm_uart_write(CM_UART_DEV_0, payload+i, 256, 500);
//         i += 256;
//         osDelay(5);
//     }
//     cm_uart_write(CM_UART_DEV_0, payload+i, 256, 500);
// #else
//     for(;i<payload_len;)
//     {
//         cm_uart_write(CM_UART_DEV_1, payload+i, 256, 500);
//         i += 256;
//         osDelay(5);
//     }
//     cm_uart_write(CM_UART_DEV_1, payload+i, 256, 500);
// #endif
    
    //int printf_size = payload_len > 128?128:payload_len;
    //cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , recv: %.*s\r\n", i, printf_size, payload);

    return 0;
}


/**
 *  \brief client->server发布消息ack回调
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] msgid 消息ID
 *  \param [in] dup dup标志
 *  \return 成功返回0，失败返回-1
 *  
 *  \details More details
 */
static int __mqtt_manager_default_puback_cb(cm_mqtt_client_t* client, unsigned short msgid, char dup)
{
    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }
    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , pub_ack: %d,%d\r\n", i, msgid, dup);
    return 0;
}


/**
 *  \brief client->server发布消息recv回调
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] msgid 消息ID
 *  \param [in] dup dup标志
 *  \return 成功返回0，失败返回-1
 *  
 *  \details More details
 */
static int __mqtt_manager_default_pubrec_cb(cm_mqtt_client_t* client, unsigned short msgid, char dup)
{
    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }
    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , pub_rec: %d,%d\r\n", i, msgid, dup);
    return 0;
}


/**
 *  \brief client->server发布消息comp回调
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] msgid 消息ID
 *  \param [in] dup dup标志
 *  \return 成功返回0，失败返回-1
 *  
 *  \details More details
 */
static int __mqtt_manager_default_pubcomp_cb(cm_mqtt_client_t* client, unsigned short msgid, char dup)
{
    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }
    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , pub_comp: %d,%d\r\n", i, msgid, dup);
    return 0;
}


/**
 *  \brief 订阅ack回调
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] msgid 消息ID
 *  \return 成功返回0，失败返回-1
 *  
 *  \details More details
 */
static int __mqtt_manager_default_suback_cb(cm_mqtt_client_t* client, unsigned short msgid, int count, int qos[])
{
    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }
    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , sub_ack: %d\r\n", i, msgid);
    return 0;
}


/**
 *  \brief 取消订阅ack回调
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] msgid 消息ID
 *  \return 成功返回0，失败返回-1
 *  
 *  \details More details
 */
static int __mqtt_manager_default_unsuback_cb(cm_mqtt_client_t* client, unsigned short msgid)
{
    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }
    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , unsub_ack: %d\r\n", i, msgid);
    return 0;
}


/**
 *  \brief ping回调
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] ret 消息状态，0：ping成功，1：ping超时
 *  \return 成功返回0，失败返回-1
 *  
 *  \details More details
 */
static int __mqtt_manager_default_pingresp_cb(cm_mqtt_client_t* client, int ret)
{
    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }
    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , pingrsp: %d\r\n", i, ret);

    return 0;
}


/**
 *  \brief 消息超时回调，包括publish/subscribe/unsubscribe等
 *  
 *  \param [in] client mqtt客户端
 *  \param [in] msgid 消息ID
 *  \return 成功返回0，失败返回-1
 *  
 *  \details More details
 */
static int __mqtt_manager_default_timeout_cb(cm_mqtt_client_t* client, unsigned short msgid)
{
    int i = 0;
    for (; i < CM_MQTT_CLIENT_MAX; i++){
        if(_mqtt_client[i] == client){
            break;
        }
    }
    cm_demo_printf("\r\n[MQTT]CM MQTT index[%d] , timeout: %d\r\n", i, msgid);
    return 0;
}

static void __cm_get_sub_topic(cm_mqtt_client_t* client)
{
    linklist_t* list = cm_mqtt_client_get_sub_topics(client);//获取topic列表
    OCTEST_FUNC_EXPECT_NOT_NULL(list,cm_mqtt_client_get_sub_topics);
    if(list == NULL || list->count == 0)
    {
        return;
    }

    char topic_tmp[200] = {0};
    int tmp_len = 0;
    int sub_topic_count = 0;
    linklist_element_t* element = NULL;
    cm_mqtt_topic_t* topic_msg = NULL;

    while((element = linklist_next_element(list, &element)) != NULL)    //取出元素
    {
        topic_msg = (cm_mqtt_topic_t*)element->content;

        if(topic_msg->state != CM_MQTT_TOPIC_SUBSCRIBED)
        {
            continue;
        }
        /* 已订阅 */
        sub_topic_count++;
        memcpy(topic_tmp + tmp_len, topic_msg->topic, topic_msg->topic_len);
        tmp_len += topic_msg->topic_len;
        tmp_len += snprintf(topic_tmp + tmp_len, sizeof(topic_tmp) - tmp_len, ",%d,", topic_msg->qos);
    }
    cm_demo_printf("tmp_len:%d\n",tmp_len);
    topic_tmp[tmp_len - 1] = '\0';

    if(sub_topic_count > 0)
    {
        cm_demo_printf("\r\n%s\r\n", topic_tmp);
    }
}

/*
 *  MQTT功能调试使用示例
 *  测试命令:CM:MQTT_OPERATION:ssl_enable:client_index:opt
 *  ssl_enable取0-1，0代表非加密传输，1代表加密传输
 *  client_index取0-4，代表要使用的client对象通道，demo目前仅有1通道配置了加密传输，客户可自行添加修改
 *  opt取0-7代表操作项，0-7依次代表client初始化、连接、订阅、发布、取消订阅、获取当前订阅主题、断开连接和销毁client
 *  注意：MQTT模块底层依赖asocketh、async_dns和eloop模块，使用MQTT功能时应先初始化前述模块
 */
void cm_test_mqtt_operation(unsigned char *cmd[], int len) 
{
    
    // if (5 != len) {
    //     cm_demo_printf("[MQTT]INVALID PARAM NUMBER\n");
    //     return;
    // }
    
    if(NULL != cmd[5])
    {
        if(0 == strcmp(cmd[5],"keepalive"))
        {
            cmqtt_glob_cfgkeepAlive = atoi(cmd[6]);
        }
    }

    int ssl_type = atoi(cmd[2]);


    int opt = atoi(cmd[4]);
    int index = atoi(cmd[3]);
    switch(opt){
        /* 初始化client */
        case 0:{
            
            if(index == 1 && ssl_type == 0){
                cm_demo_printf("\r\n[MQTT]INVALID PARAM!!\r\n");
                break;
            }
            
            if(_mqtt_client[index]){
                cm_demo_printf("\r\n[MQTT]CM MQTT !CLIENT IS RUN!!\r\n");
                break;
            }
            _mqtt_client[index] = cm_mqtt_client_create();  //client初始化，最大支持五个实例
            OCTEST_FUNC_EXPECT_NOT_NULL(_mqtt_client[index],cm_mqtt_client_create);
            if (NULL == _mqtt_client[index]){
                cm_demo_printf("\r\n[MQTT]CM MQTT CREATE CLIENT ERROR!!!\r\n");
                break;
            }
            /* 设置回调函数 */
            cm_mqtt_client_cb_t callback = {0};
            callback.connack_cb = __mqtt_manager_default_connack_cb;
            callback.publish_cb = __mqtt_manager_default_publish_cb;
            callback.puback_cb = __mqtt_manager_default_puback_cb;
            callback.pubrec_cb = __mqtt_manager_default_pubrec_cb;
            callback.pubcomp_cb = __mqtt_manager_default_pubcomp_cb;
            callback.suback_cb = __mqtt_manager_default_suback_cb;
            callback.unsuback_cb = __mqtt_manager_default_unsuback_cb;
            callback.pingresp_cb = __mqtt_manager_default_pingresp_cb;
            callback.timeout_cb = __mqtt_manager_default_timeout_cb;

            /* 设置client参数 */
            int version = 4;//版本3.1.1
            int pkt_timeout = 10;//发送超时10秒
            int reconn_times = 3;//重连三次
            int reconn_cycle = 20;//重连间隔20秒
            int reconn_mode = 0;//以固定间隔尝试重连
            int retry_times = 3;//重传三次
            int ping_cycle = 30;//ping周期30秒
            int dns_priority = 2;//MQTT dns解析ipv6优先

        	cm_demo_printf("mqtt 1 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_EVENT, (void*)&callback));
            cm_demo_printf("mqtt 2 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_VERSION, (void*)&version));
            cm_demo_printf("mqtt 3 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_PKT_TIMEOUT, (void*)&pkt_timeout));
            cm_demo_printf("mqtt 4 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_RETRY_TIMES, (void*)&retry_times));
            cm_demo_printf("mqtt 5 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_RECONN_MODE, (void*)&reconn_mode));
            cm_demo_printf("mqtt 6 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_RECONN_TIMES, (void*)&reconn_times));
            cm_demo_printf("mqtt 7 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_RECONN_CYCLE, (void*)&reconn_cycle));

            cm_demo_printf("mqtt 7 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_PING_CYCLE, (void*)&ping_cycle));
            int mqtt_client_set_opt = cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_DNS_PRIORITY, (void*)&dns_priority);
            cm_demo_printf("mqtt 7 cm_mqtt_client_set_opt ret:%d\n",mqtt_client_set_opt);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_set_opt,cm_mqtt_client_set_opt);

            
            /* SSL设置 */
            if (ssl_type){
                if (index == 1){
                    int32_t ssl_id = 1;
                    int32_t verify = 1;
                    int32_t negotime = 10;
                    int ssl_enable = 1;
                    cm_demo_printf("mqtt 1 cm_ssl_setopt ret:%d\n", cm_ssl_setopt(ssl_id, CM_SSL_PARAM_VERIFY, &verify));
                    cm_demo_printf("mqtt 2 cm_ssl_setopt ret:%d\n", cm_ssl_setopt(ssl_id, CM_SSL_PARAM_NEGOTIME, &negotime));
                    cm_demo_printf("mqtt 3 cm_ssl_setopt ret:%d\n", cm_ssl_setopt(ssl_id, CM_SSL_PARAM_CA_CERT, onenet_ca_cert));
                    cm_demo_printf("mqtt 8 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_SSL_ID, &ssl_id));
                    cm_demo_printf("mqtt 9 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[index], CM_MQTT_OPT_SSL_ENABLE, &ssl_enable));
                }
            }
            
        }break;

        /* 连接 */
        case 1:{
            if(ssl_type && 1 == index){
                /* 配置连接参数，对于字符串参数，内部仅保留指针，不分配空间 */
                cm_mqtt_connect_options_t conn_options = {
                    .hostport = (unsigned short)cmqtt_glob_cfgport_onenet_tls,
                    .hostname = (const char*)cmqtt_glob_cfgserver_onenet_tls,
                    .clientid = (const char*)cmqtt_glob_cfgclient_id_onenet_tls,
                    .username = (const char*)cmqtt_glob_cfguser_onenet_tls,
                    .password = (const char*)cmqtt_glob_cfgpasswd_onenet_tls,
                    .keepalive = (unsigned short)cmqtt_glob_cfgkeepAlive,
                    .will_topic = NULL,
                    .will_message = NULL,
                    .will_flag = 0, //若要使用遗嘱机制请置1，并补充相关遗嘱信息
                    .clean_session = (char)cmqtt_glob_cfgclean,
                    };
                int mqtt_client_connect = cm_mqtt_client_connect(_mqtt_client[index], &conn_options);
                cm_demo_printf("mqtt 1 cm_mqtt_client_connect ret:%d\n",mqtt_client_connect);//连接
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_connect,cm_mqtt_client_connect);
            }
            if(2 == index){
                /* 配置连接参数，对于字符串参数，内部仅保留指针，不分配空间 */
                cm_mqtt_connect_options_t conn_options = {
                    .hostport = (unsigned short)cmqtt_glob_cfgport_onenet_,
                    .hostname = (const char*)cmqtt_glob_cfgserver_onenet_,
                    .clientid = (const char*)cmqtt_glob_cfgclient_id_onenet_,
                    .username = (const char*)cmqtt_glob_cfguser_onenet_,
                    .password = (const char*)cmqtt_glob_cfgpasswd_onenet_,
                    .keepalive = (unsigned short)cmqtt_glob_cfgkeepAlive,
                    .will_topic = NULL,
                    .will_message = NULL,
                    .clean_session = (char)cmqtt_glob_cfgclean,
                    };
                int mqtt_client_connect = cm_mqtt_client_connect(_mqtt_client[index], &conn_options);
                cm_demo_printf("mqtt 2 cm_mqtt_client_connect ret:%d\n",mqtt_client_connect);//连接
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_connect,cm_mqtt_client_connect);
            }
            if(3 == index){
                /* 配置连接参数，对于字符串参数，内部仅保留指针，不分配空间 */
                cm_mqtt_connect_options_t conn_options = {
                    .hostport = (unsigned short)cmqtt_glob_cfgport_onenet__,
                    .hostname = (const char*)cmqtt_glob_cfgserver_onenet__,
                    .clientid = (const char*)cmqtt_glob_cfgclient_id_onenet__,
                    .username = (const char*)cmqtt_glob_cfguser_onenet__,
                    .password = (const char*)cmqtt_glob_cfgpasswd_onenet__,
                    .keepalive = (unsigned short)cmqtt_glob_cfgkeepAlive,
                    .will_topic = NULL,
                    .will_message = NULL,
                    .clean_session = (char)cmqtt_glob_cfgclean,
                    };
                int mqtt_client_connect = cm_mqtt_client_connect(_mqtt_client[index], &conn_options);
                cm_demo_printf("mqtt 3 cm_mqtt_client_connect ret:%d\n",mqtt_client_connect);//连接
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_connect,cm_mqtt_client_connect);
            }

            if(0 == index){
                /* 配置连接参数，对于字符串参数，内部仅保留指针，不分配空间 */
                cm_mqtt_connect_options_t conn_options = {
                    .hostport = (unsigned short)cmqtt_glob_cfgport_test,
                    .hostname = (const char*)cmqtt_glob_cfgserver_test,
                    .clientid = (const char*)cmqtt_glob_cfgclient_id_test1,
                    .username = (const char*)cmqtt_glob_cfguser_test1,
                    .password = (const char*)cmqtt_glob_cfgpasswd_test1,
                    .keepalive = (unsigned short)cmqtt_glob_cfgkeepAlive,
                    .will_topic = cmmqtt_glob_willlwt_subtopic_test,
                    .will_message = cmmqtt_glob_willlwt_pubmessage_str,
                    .clean_session = (char)cmqtt_glob_cfgclean,
                    .will_flag = 1,
                    };
                int mqtt_client_connect = cm_mqtt_client_connect(_mqtt_client[index], &conn_options);
                cm_demo_printf("mqtt 0 cm_mqtt_client_connect ret:%d\n",mqtt_client_connect);//连接
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_connect,cm_mqtt_client_connect);
            }

            if(4 == index){
                /* 配置连接参数，对于字符串参数，内部仅保留指针，不分配空间 */
                cm_mqtt_connect_options_t conn_options = {
                    .hostport = (unsigned short)cmqtt_glob_cfgport_test,
                    .hostname = (const char*)cmqtt_glob_cfgserver_test,
                    .clientid = (const char*)cmqtt_glob_cfgclient_id_test2,
                    .username = (const char*)cmqtt_glob_cfguser_test2,
                    .password = (const char*)cmqtt_glob_cfgpasswd_test2,
                    .keepalive = (unsigned short)cmqtt_glob_cfgkeepAlive,
                    .will_topic = NULL,
                    .will_message = NULL,
                    .clean_session = (char)cmqtt_glob_cfgclean,
                    };
                int mqtt_client_connect = cm_mqtt_client_connect(_mqtt_client[index], &conn_options);
                cm_demo_printf("mqtt 4 cm_mqtt_client_connect ret:%d\n",mqtt_client_connect);//连接
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_connect,cm_mqtt_client_connect);
            }

        }break;

        /* 发布 */
        case 3:{
            uint8_t publish_flags = 0;
            publish_flags |= CM_MQTT_PUBLISH_QOS_1;
            char *pubmessage = cmmqtt_glob_pubmessage_4k;
            //char *pubmessage = "1234567890";
            if(1 == index && ssl_type){
                pubmessage = cmmqtt_glob_pubmessage_json;
                int ret = cm_mqtt_client_publish(_mqtt_client[index], cmmqtt_glob_pubtopic_onenet_tls, pubmessage, strlen(pubmessage), publish_flags);
                cm_demo_printf("mqtt 1 cm_mqtt_client_publish ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT publish ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,ret,cm_mqtt_client_publish);
            }
            else{
                char *topic_tmp[1] = {0};
                if (0 == index || 4 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_willlwt_subtopic_test;
                }
                else if(3 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_subtopic_onenet__;
                }
                else if(2 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_pubtopic_onenet_;
                }
                int ret = cm_mqtt_client_publish(_mqtt_client[index], topic_tmp[0], pubmessage, strlen(pubmessage), publish_flags);
                cm_demo_printf("mqtt 2 cm_mqtt_client_publish ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT publish ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,ret,cm_mqtt_client_publish);
            }
            
        }break;
        /* 订阅 */
        case 2:{
            if(1 == index && ssl_type){
                char *topic_tmp[1] = {0};
                topic_tmp[0] = cmmqtt_glob_subtopic_onenet_tls;
                char qos_tmp[1] = {0};
                qos_tmp[0] = 0;
                int ret = cm_mqtt_client_subscribe(_mqtt_client[index], (const char**)topic_tmp, qos_tmp, 1);
				cm_demo_printf("mqtt 1 cm_mqtt_client_subscribe ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT subscribe ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mqtt_client_subscribe);
            }

            else{
                char *topic_tmp[1] = {0};
                if (0 == index || 4 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_willlwt_subtopic_test;
                }
                else if(3 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_subtopic_onenet__;
                }
                else if(2 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_subtopic_onenet_;
                }
                char qos_tmp[1] = {0};
                qos_tmp[0] = 0;
                int ret = cm_mqtt_client_subscribe(_mqtt_client[index], (const char**)topic_tmp, qos_tmp, 1);
				cm_demo_printf("mqtt 2 cm_mqtt_client_subscribe ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT subscribe ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mqtt_client_subscribe);
            }
            
        }break;
        /* 取消订阅 */
        case 4:{

            if(1 == index && ssl_type){
                char *topic_tmp[1] = {0};
                topic_tmp[0] = cmmqtt_glob_subtopic_onenet_tls;
                int ret = cm_mqtt_client_unsubscribe(_mqtt_client[index], (const char**)topic_tmp, 1);
				cm_demo_printf("mqtt 1 cm_mqtt_client_unsubscribe ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT unsubscribe ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mqtt_client_unsubscribe);
            }

            else{
                char *topic_tmp[1] = {0};
                if (0 == index || 4 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_willlwt_subtopic_test;
                }
                else if(3 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_subtopic_onenet__;
                }
                else if(2 == index)
                {
                    topic_tmp[0] = cmmqtt_glob_subtopic_onenet_;
                }
                int ret = cm_mqtt_client_unsubscribe(_mqtt_client[index], (const char**)topic_tmp, 1);
                cm_demo_printf("mqtt 2 cm_mqtt_client_unsubscribe ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT unsubscribe ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mqtt_client_unsubscribe);
            }
            
        }break;

        /* 查看topic */
        case 5:{
            __cm_get_sub_topic(_mqtt_client[index]);
        }break;

        /* 关闭连接 */
        case 6:{
            int mqtt_client_disconnect = cm_mqtt_client_disconnect(_mqtt_client[index]);
            cm_demo_printf("mqtt cm_mqtt_client_disconnect ret:%d\n",mqtt_client_disconnect);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_disconnect,cm_mqtt_client_disconnect);
        }break;

        /* 释放client */
        case 7:{
            if(_mqtt_client[index] == NULL)
            {
                cm_demo_printf("mqtt _mqtt_client[%d] already destory!\n",index);
                break;
            }
            int mqtt_client_destroy = cm_mqtt_client_destroy(_mqtt_client[index]);
            cm_demo_printf("mqtt cm_mqtt_client_destroy ret:%d\n",mqtt_client_destroy);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_destroy,cm_mqtt_client_destroy);
            _mqtt_client[index] = NULL;
        }break;
        //获取状态
        case 8:
        {
            int mqtt_client_get_state = cm_mqtt_client_get_state(_mqtt_client[index]);
            cm_demo_printf("mqtt cm_mqtt_client_get_state ret:%d\n",mqtt_client_get_state);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,mqtt_client_get_state,cm_mqtt_client_get_state);
            break;
        }
        //获取消息id
        case 9:
        {
            int mqtt_client_get_msgid = cm_mqtt_client_get_msgid(_mqtt_client[index]);
            cm_demo_printf("mqtt cm_mqtt_client_get_msgid ret:%d\n",mqtt_client_get_msgid);
            OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,mqtt_client_get_msgid,cm_mqtt_client_get_msgid);
            break;
        }
    }
    if(0 == strcmp("TRAVERSAL",cmd[2]))
    {
        for(int i = 3;i < MQTT_TRAVERSAL_SIZE;++i)
        {
            cm_demo_printf("the %d test\n");
            char * cmqtt_traversal_id = (char * )cm_malloc((i / 3)+1); 
            char * cmqtt_traversal_user = (char * )cm_malloc(((i + 1) / 3)+1);
            char * cmqtt_traversal_passwd = (char * )cm_malloc(((i + 2) / 3)+1);
            if(NULL == cmqtt_traversal_id || NULL == cmqtt_traversal_user || NULL == cmqtt_traversal_passwd)
            {
                cm_demo_printf("cm_malloc fail\n");
                return;
            }
            memcpy(cmqtt_traversal_id,cmmqtt_glob_pubmessage_1k,(i / 3));
            memcpy(cmqtt_traversal_user,cmmqtt_glob_pubmessage_1k,((i + 1) / 3));
            memcpy(cmqtt_traversal_passwd,cmmqtt_glob_pubmessage_1k,((i + 2) / 3));
            cm_demo_printf("cmqtt_traversal_id:%s\n",cmqtt_traversal_id);
            cm_demo_printf("cmqtt_traversal_user:%s\n",cmqtt_traversal_user);
            cm_demo_printf("cmqtt_traversal_passwd:%s\n",cmqtt_traversal_passwd);
            _mqtt_client[4] = cm_mqtt_client_create();  //client初始化
            OCTEST_FUNC_EXPECT_NOT_NULL(_mqtt_client[4],cm_mqtt_client_create);
            if (NULL == _mqtt_client[4]){
                cm_demo_printf("\r\n[MQTT]CM MQTT CREATE CLIENT ERROR!!!\r\n");
                cm_free(cmqtt_traversal_id);
                cm_free(cmqtt_traversal_user);
                cm_free(cmqtt_traversal_passwd);
                return;
            }
            osDelay(100);
            /* 设置回调函数 */
            cm_mqtt_client_cb_t callback = {0};
            callback.connack_cb = __mqtt_manager_default_connack_cb;
            callback.publish_cb = __mqtt_manager_default_publish_cb;
            callback.puback_cb = __mqtt_manager_default_puback_cb;
            callback.pubrec_cb = __mqtt_manager_default_pubrec_cb;
            callback.pubcomp_cb = __mqtt_manager_default_pubcomp_cb;
            callback.suback_cb = __mqtt_manager_default_suback_cb;
            callback.unsuback_cb = __mqtt_manager_default_unsuback_cb;
            callback.pingresp_cb = __mqtt_manager_default_pingresp_cb;
            callback.timeout_cb = __mqtt_manager_default_timeout_cb;

            /* 设置client参数 */
            int version = 4;//版本3.1.1
            int pkt_timeout = 10;//发送超时10秒
            int reconn_times = 3;//重连三次
            int reconn_cycle = 20;//重连间隔20秒
            int reconn_mode = 0;//以固定间隔尝试重连
            int retry_times = 3;//重传三次
            int ping_cycle = 30;//ping周期30秒
            int dns_priority = 2;//MQTT dns解析ipv6优先

        	cm_demo_printf("cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_EVENT, (void*)&callback));
            cm_demo_printf("cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_VERSION, (void*)&version));
            cm_demo_printf("cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_PKT_TIMEOUT, (void*)&pkt_timeout));
            cm_demo_printf("cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_RETRY_TIMES, (void*)&retry_times));
            cm_demo_printf("cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_RECONN_MODE, (void*)&reconn_mode));
            cm_demo_printf("cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_RECONN_TIMES, (void*)&reconn_times));
            cm_demo_printf("cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_RECONN_CYCLE, (void*)&reconn_cycle));

            cm_demo_printf("mqtt 7 cm_mqtt_client_set_opt ret:%d\n", cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_PING_CYCLE, (void*)&ping_cycle));
            int mqtt_client_set_opt = cm_mqtt_client_set_opt(_mqtt_client[4], CM_MQTT_OPT_DNS_PRIORITY, (void*)&dns_priority);
            cm_demo_printf("mqtt 7 cm_mqtt_client_set_opt ret:%d\n",mqtt_client_set_opt);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_set_opt,cm_mqtt_client_set_opt);

            osDelay(100);
            /* 配置连接参数，对于字符串参数，内部仅保留指针，不分配空间 */
                cm_mqtt_connect_options_t conn_options = {
                    .hostport = (unsigned short)cmqtt_glob_cfgport_test,
                    .hostname = (const char*)cmqtt_glob_cfgserver_test,
                    .clientid = (const char*)cmqtt_traversal_id,
                    .username = (const char*)cmqtt_traversal_user,
                    .password = (const char*)cmqtt_traversal_passwd,
                    .keepalive = (unsigned short)cmqtt_glob_cfgkeepAlive,
                    .will_topic = NULL,
                    .will_message = NULL,
                    .clean_session = (char)cmqtt_glob_cfgclean,
                    };
                int mqtt_client_connect = cm_mqtt_client_connect(_mqtt_client[4], &conn_options);
                cm_demo_printf("mqtt 4 cm_mqtt_client_connect ret:%d\n",mqtt_client_connect);//连接
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_connect,cm_mqtt_client_connect);
                osDelay(150);
                /*订阅*/
                char *topic_tmp[1] = {0};
                topic_tmp[0] = cmmqtt_glob_willlwt_subtopic_test;
                char qos_tmp[1] = {0};
                qos_tmp[0] = 0;
                int ret = cm_mqtt_client_subscribe(_mqtt_client[4], (const char**)topic_tmp, qos_tmp, 1);
				cm_demo_printf("cm_mqtt_client_subscribe ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT subscribe ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mqtt_client_subscribe);
                osDelay(200);
                /*发布*/
                char *pubmessage = cmmqtt_glob_pubmessage_4k;
                topic_tmp[0] = cmmqtt_glob_willlwt_subtopic_test;
                ret = cm_mqtt_client_publish(_mqtt_client[4], topic_tmp[0], pubmessage, strlen(pubmessage), 'a');
                cm_demo_printf("cm_mqtt_client_publish ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT publish ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,ret,cm_mqtt_client_publish);
                osDelay(200);
                /*取消订阅*/
                topic_tmp[0] = cmmqtt_glob_willlwt_subtopic_test;
                ret = cm_mqtt_client_unsubscribe(_mqtt_client[4], (const char**)topic_tmp, 1);
                cm_demo_printf("cm_mqtt_client_unsubscribe ret:%d\n",ret);
                if (0 > ret){
                    cm_demo_printf("\r\n[MQTT]CM MQTT unsubscribe ERROR!!!, ret = %d\r\n", ret);
                }
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mqtt_client_unsubscribe);
                osDelay(100);
                /* 关闭连接 */
                int mqtt_client_disconnect = cm_mqtt_client_disconnect(_mqtt_client[4]);
                cm_demo_printf("mqtt cm_mqtt_client_disconnect ret:%d\n",mqtt_client_disconnect);
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_disconnect,cm_mqtt_client_disconnect);
                osDelay(100);
                /* 释放client */
                if(_mqtt_client[4] == NULL)
                {
                    cm_demo_printf("mqtt _mqtt_client[%d] already destory!\n",4);
                    cm_free(cmqtt_traversal_id);
                    cm_free(cmqtt_traversal_user);
                    cm_free(cmqtt_traversal_passwd);
                    return;
                }
                int mqtt_client_destroy = cm_mqtt_client_destroy(_mqtt_client[4]);
                cm_demo_printf("mqtt cm_mqtt_client_destroy ret:%d\n",mqtt_client_destroy);
                OCTEST_FUNC_EXPECT_EQUAL_INT32(0,mqtt_client_destroy,cm_mqtt_client_destroy);
                _mqtt_client[4] = NULL;
                cm_free(cmqtt_traversal_id);
                cm_free(cmqtt_traversal_user);
                cm_free(cmqtt_traversal_passwd);
                osDelay(400);
        }
    }
}




